package cn._51doit.flink.day02.transformations;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.typeinfo.TypeHint;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.operators.StreamFlatMap;
import org.apache.flink.util.Collector;

/**
 * flatMap底层实现
 *
 *
 */
public class FlatMapDemo2 {

    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        //hadoop flink hadoop spark
        DataStreamSource<String> lines = env.socketTextStream("localhost", 8888);

        //(hadoop, 1)
        //(flink, 1)
        //(hadoop, 1)
        FlatMapFunction<String, Tuple2<String, Integer>> flatMapFunction = new FlatMapFunction<String, Tuple2<String, Integer>>() {

            @Override
            public void flatMap(String line, Collector<Tuple2<String, Integer>> collector) throws Exception {
                String[] words = line.split(" ");
                for (String word : words) {
                    collector.collect(Tuple2.of(word, 1));
                }
            }
        };

        SingleOutputStreamOperator<Tuple2<String, Integer>> wordAndOne = lines.transform("MyFlatMap", TypeInformation.of(new TypeHint<Tuple2<String, Integer>>() {
        }), new StreamFlatMap<>(flatMapFunction));

        wordAndOne.print();

        env.execute();
    }

}
